package utils;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

public class AutoTestUtils {
    public static WebDriver driver;
    public static void createDriver() {
        WebDriverManager.chromedriver().setup();
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--remote-allow-origins=*");
        if(driver == null) {
            driver = new ChromeDriver(chromeOptions);
        }
    }

    public AutoTestUtils(String url) {
        createDriver();
        driver.get(url);
    }
    public void quitPage() {
        driver.quit();
    }
    public void getTakePhoto(String className,String methodName) throws IOException {
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyy-MM-dd");//年月日
        SimpleDateFormat sim2 = new SimpleDateFormat("HHmmssSS");
        String directoryTime = sim1.format(System.currentTimeMillis());
        String fileTime = sim2.format(System.currentTimeMillis());
        //./src/test/image/2024-08-30/test07-16562326
        String fileName = "./src/test/image/" + directoryTime + "/"+ className + "-" + methodName + fileTime + ".png";
        System.out.println("fileName:"+ fileName);
        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File(fileName));
    }

}
